﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Platform.Model.Validations
{
	/// <summary>
	/// Lop chua ket qua thuc thi mot phuong thuc.
	/// Phuong thuc nay khong can tra ve doi tuong
	/// </summary>
	public class ActionResult
	{
		/// <summary>
		/// Cac ngoai le xay ra khi thuc thi phuong thuc
		/// </summary>
		public List<Exception> Exceptions { get; set; }

		/// <summary>
		/// Cho biet viec thuc thi thanh cong hay khong
		/// </summary>
		public bool Success
		{
			get { return !Exceptions.Any(); }
		}

		/// <summary>
		/// Khoi tao doi tuong luu ket qua thuc thi
		/// </summary>
		public ActionResult()
		{
			Exceptions = new List<Exception>();
		}

		/// <summary>
		/// Lay danh sach loi xay ra trong qua trinh thuc thi
		/// </summary>
		/// <returns>Chuoi mo ta thong bao loi</returns>
		public string GetErrorMessages()
		{
			var sb = new StringBuilder();
			Exceptions.ForEach(e => sb.AppendLine(e.Message));
			return sb.ToString();
		}
	}

	/// <summary>
	/// Lop chua ket qua thuc thi mot phuong thuc.
	/// Phuong thuc nay phai tra ve mot doi tuong
	/// </summary>
	/// <typeparam name="T">Kieu cua doi tuong tra ve</typeparam>
	public class ActionResult<T> : ActionResult
	{
		public T Result { get; set; }
	}

	/// <summary>
	/// Lop chua ket qua thuc thi mot phuong thuc.
	/// Phuong thuc nay tra ve mot danh sach cac doi tuong
	/// </summary>
	/// <typeparam name="T">Kieu cua doi tuong tra ve</typeparam>
	public class ActionResultAsList<T> : ActionResult
	{
		public List<T> Result { get; set; }
	}
}
